package top.v5it.japi.plus.common.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

/**
 * @author zhanpu
 */
public class JackSonUtil {

    private static final ObjectMapper MAPPER;

    static {
        MAPPER = new ObjectMapper()
                //序列化时，过虑为null的属性值不参与序列化
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
                .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
                //设置日期格式化
//                .setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"))
                //反序列化时未知属性不参与
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                //加入JavaTimeModule
                .registerModule(new JavaTimeModule());
    }

    private JackSonUtil() {
        throw new UnsupportedOperationException();
    }

    public static ObjectMapper getMapper() {
        return MAPPER;
    }
}
